package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.MaterialPurchaseRecordStatus;
import com.jiantong.work.impl.dao.MaterialPurchaseRecordDao;
import com.jiantong.work.model.MaterialPurchaseRecord;
import com.jiantong.work.service.MaterialPurchaseRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by maowenjie on 2017/4/13.
 *
 */
@Slf4j
@Service
@RpcProvider
public class MaterialPurchaseRecordReadServiceImpl implements MaterialPurchaseRecordReadService {

    private final MaterialPurchaseRecordDao materialPurchaseRecordDao;
    private final BreedFactoryDao breedFactoryDao;
    @Autowired
    public MaterialPurchaseRecordReadServiceImpl(MaterialPurchaseRecordDao materialPurchaseRecordDao, BreedFactoryDao breedFactoryDao) {
        this.materialPurchaseRecordDao = materialPurchaseRecordDao;
        this.breedFactoryDao = breedFactoryDao;
    }

    @Override
    public Response<MaterialPurchaseRecord> findMaterialPurchaseRecordById(Long id) {
        try{
            MaterialPurchaseRecord materialPurchaseRecor = materialPurchaseRecordDao.findById(id);
            if(materialPurchaseRecor == null){
                log.error("quarantineCenterHarmless(id={}) is not found", id);
                return Response.fail("quarantineCenterHarmless.not.found");
            }
            return Response.ok(materialPurchaseRecor);
        }catch (Exception e){
            log.error("failed to find quarantineCenterHarmless(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("quarantineCenterHarmless.find.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            List<MaterialPurchaseRecord> status = materialPurchaseRecordDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("materialPurchaseRecord status (id={}) is not found", id);
                return Response.fail("materialPurchaseRecord.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("materialPurchaseRecord status (id={}) is not found", id);
            return Response.fail("materialPurchaseRecord.status.not.found");
        }
    }

    @Override
    public Response<Paging<MaterialPurchaseRecord>> pagingMaterialPurchaseRecordByStatus(Integer pageNo, Integer pageSize,Long breedFactoryId, Integer status) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if (status !=null) {
                criteria.put("status", status);
            }
            criteria.put("breedFactoryId",breedFactoryId);
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(materialPurchaseRecordDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find materialPurchaseRecord(id={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("materialPurchaseRecord.find.fail");
        }
    }

    @Override
    public Response<Paging<MaterialPurchaseRecord>>pagingMaterialPurchaseRecordByUser(Integer pageNo,Integer pageSize) {

        List<Long> breedFactoryIds = new ArrayList<>();
        LoginUser loginUser = UserUtil.getCurrentUser();
        breedFactoryIds = loginUser.getBreedFactoryIds();

        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            criteria.put("status", 1);  //记录的生效状态
            criteria.put("breedFactoryIds",breedFactoryIds);    //当前登录人管辖的养殖场id
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(materialPurchaseRecordDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find materialPurchaseRecord(id={}), cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("materialPurchaseRecord.find.fail");
        }
    }

    @Override
    public Response<Paging<MaterialPurchaseRecord>> pagingMaterialPurchaseRecordGqv(Integer pageNo, Integer pageSize, Long breedFactoryId,Integer breedType, Date fromDate, Date toDate) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            List<Long> breedFactoryIds = new ArrayList<>();
            if(breedFactoryId!=null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            //在这里根据breedType赛选一下养殖场,从breedFactoryIds中去掉不是这个养殖类型的养殖场ID
            if (breedType != null) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("breedType",breedType);
                if(!breedFactoryIds.isEmpty()) {
                    map.put("breedFactoryIds", breedFactoryIds);
                    breedFactoryIds = breedFactoryDao.selectedByType(map);
                }
            }

            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds", breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            if(fromDate!=null && toDate!=null){
                criteria.put("fromDate",fromDate);
                criteria.put("toDate",toDate);
            }
            criteria.put("status", MaterialPurchaseRecordStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(materialPurchaseRecordDao.pagingMaterialPurchaseRecordGqv(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find MaterialPurchaseRecord fromDate={},fromDate={},toDate={}),cause:{}",breedFactoryId, fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("MaterialPurchaseRecord.find.fail");
        }
    }

    @Override
    public Response<Paging<MaterialPurchaseRecord>> pagingMaterialPurchaseRecordByKeyWords(Integer pageNo, Integer pageSize, Map criteria) {
        PageInfo pageInfo = new PageInfo(pageNo, pageSize);
        criteria.put("status", MaterialPurchaseRecordStatus.FINISHED.value());
        criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
        return Response.ok(materialPurchaseRecordDao.pagingMaterialPurchaseRecordGqv(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
    }

    @Override
    public Response<Double> sumTotalPriceForWeb(Map<String, Object> map) {
        try{
            return Response.ok(materialPurchaseRecordDao.sumTotalPriceForWeb(map));
        }catch (Exception e){
            log.error("failed to sum total price , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to sum total price");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findMaterialForWeb(Map<String, Object> map) {
        try{
            return Response.ok(materialPurchaseRecordDao.findMaterialForWeb(map));
        }catch (Exception e){
            log.error("failed to find material info , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find material info");
        }
    }
}
